package
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import it.gotoandplay.smartfoxserver.SFSEvent;
	
	public class RaceScreen extends RaceMenu
	{
		
		private var resultPopUp:RaceResultPopUp;
		
		private var players:Array;
		private var textFields:Sprite;
		
		private var playerToRace:Player;
		private var selectedPlayer:Player;
		private var selectedTextFieldContainer:Object;
		
		public function RaceScreen(game:DRace)
		{
			super();
			this.header.btnClose.addEventListener(MouseEvent.CLICK,game.onRaceClosed);
			this.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStage);
			this.resultCanvas.btnRace.addEventListener(MouseEvent.CLICK,onButtonRaceClicked);
			textFields = new Sprite();
			players = new Array();
			this.playerListCanvas.addChild(textFields);
			
			resultPopUp = new RaceResultPopUp();
			resultPopUp.btnClose.addEventListener(MouseEvent.CLICK,onResultPopUpClosed);
			this.resultCanvas.playerBox.labelOpponent.text = "Player";
			
		}
		public function onAddedToStage(ev:Event):void
		{
			DRace.sfc.addEventListener(SFSEvent.onExtensionResponse,onExtensionResponse);
			
			var obj:Object = {};
			obj.userId = DRace.player.id;
			
			DRace.sfc.sendXtMessage("simpleExt","getRaceOpponents",obj);	
			updateOpponent(null);
			updatePlayerBox();
		}
		private function onRemovedFromStage(ev:Event):void
		{
			//saco el listener
			DRace.sfc.removeEventListener(SFSEvent.onExtensionResponse,onExtensionResponse);
			
		}
		private function onExtensionResponse(ev:SFSEvent):void
		{
			if (ev.params.dataObj._cmd == "getRaceOpponents")
			{
				clearTextFields();
				
				for each (var opo:Object in ev.params.dataObj.opponents)
				{
					var opponent:Player = new Player();
					opponent.id = opo.id;
					opponent.name = opo.name;
					opponent.points = opo.points;
					opponent.ranking = opo.position;
					opponent.victories = opo.victories;
					opponent.losses = opo.losses;
					
					players.push(opponent);
					addTextField(opponent.ranking,opponent.name,opponent.points,opponent.victories,opponent.losses);
					
				}
			}
			else if (ev.params.dataObj._cmd == "race")
			{
				//actualizo al player y tiro el popup
				var moneyEarned:int = ev.params.dataObj.money - DRace.player.money;
				DRace.player.money = ev.params.dataObj.money;
				
				var pointsGained:int = ev.params.dataObj.points - DRace.player.points;
				DRace.player.points = ev.params.dataObj.points;
				
				DRace.player.ranking = ev.params.dataObj.position;
				DRace.player.victories = ev.params.dataObj.victories;
				DRace.player.losses = ev.params.dataObj.loses;
				DRace.player.racesPerDay = ev.params.dataObj.racesPerDay;
				
				resultPopUp.valueCurrentMoney.text = DRace.player.money.toString();
				resultPopUp.valueCurrentRank.text = DRace.player.ranking.toString();
				resultPopUp.valueMoneyEarned.text = moneyEarned.toString();
				resultPopUp.valuePointsGained.text = pointsGained.toString();
				resultPopUp.valueResult.text = "You "+ ev.params.dataObj.result;
				
				this.playerListCanvas.mouseEnabled = false;
				this.playerListCanvas.mouseChildren = false;
				this.resultCanvas.mouseChildren = false;
				this.resultCanvas.mouseEnabled = false;
				this.addChild(resultPopUp);
				
				var obj:Object = {};
				obj.userId = DRace.player.id;
				
				DRace.sfc.sendXtMessage("simpleExt","getRaceOpponents",obj);	
				updateOpponent(null);
				updatePlayerBox();
				
			}
						
			
		}
		private function createTextField(text:String,posX:int,posY:int,width:Number,height:Number):TextField
		{
			var textField:TextField = new TextField();
			textField.selectable = false;
			textField.background = true;
			textField.backgroundColor = 0x777777;
			textField.border = true;
			textField.borderColor = 0xFF0000;
			textField.width = width;
			textField.height = height;
			textField.text = text;	
			textField.x = posX;
			textField.y = posY;
			
			return textField;							
		}
		private function addTextField(ranking:int,name:String,points:int,victories:int,losses:int):void
		{
			
			const textFieldHeight:int = 25;
			var textFieldSpriteContainer:Sprite = new Sprite();
			textFieldSpriteContainer.mouseChildren = false;
			textFieldSpriteContainer.x = 2;
			textFieldSpriteContainer.y = textFields.numChildren*textFieldHeight;
			
			var rankingText:TextField = createTextField(ranking.toString(),0,0,64,textFieldHeight);
			var nameText:TextField = createTextField(name,rankingText.width + rankingText.x,0,255,textFieldHeight);	
			var pointsText:TextField = createTextField(points.toString(),nameText.width + nameText.x,0,64,textFieldHeight);	
			var victoriesText:TextField = createTextField(victories.toString(),pointsText.width + pointsText.x,0,64,textFieldHeight);	
			var lossesText:TextField = createTextField(losses.toString(),victoriesText.width + victoriesText.x,0,64,textFieldHeight);
			
			textFieldSpriteContainer.addChild(rankingText);
			textFieldSpriteContainer.addChild(nameText);
			textFieldSpriteContainer.addChild(pointsText);
			textFieldSpriteContainer.addChild(victoriesText);
			textFieldSpriteContainer.addChild(lossesText);
						
			textFieldSpriteContainer.addEventListener(MouseEvent.ROLL_OVER,onMouseOverTextField);
			textFieldSpriteContainer.addEventListener(MouseEvent.ROLL_OUT,onMouseOutTextField);
			textFieldSpriteContainer.addEventListener(MouseEvent.CLICK,onMouseClickTextField);
			
			this.textFields.addChild(textFieldSpriteContainer);
				
		}
		private function clearTextFields():void
		{
			for (var i:int = textFields.numChildren-1 ; i >=0 ; i--)
			{
				textFields.getChildAt(i).removeEventListener(MouseEvent.ROLL_OVER,onMouseOverTextField);
				textFields.getChildAt(i).removeEventListener(MouseEvent.ROLL_OUT,onMouseOutTextField);
				textFields.getChildAt(i).removeEventListener(MouseEvent.CLICK,onMouseClickTextField);
				textFields.removeChildAt(i);
			}
			players = new Array();
		}
		private function updatePlayerBox():void
		{
			resultCanvas.playerBox.valueName.text = DRace.player.userName;
			resultCanvas.playerBox.valueRanking.text = DRace.player.ranking.toString();
			resultCanvas.playerBox.valuePoints.text = DRace.player.points.toString();
			resultCanvas.playerBox.valueVictories.text = DRace.player.victories.toString();
			resultCanvas.playerBox.valueLosses.text = DRace.player.losses.toString();
			resultCanvas.valueRacesLeft.text = DRace.player.racesPerDay.toString();	
			
			if (DRace.player.racesPerDay <=0)
			{
				resultCanvas.btnRace.mouseEnabled = false;
				resultCanvas.btnRace.mouseChildren = false;
			}
			else
			{
				resultCanvas.btnRace.mouseEnabled = true;
				resultCanvas.btnRace.mouseChildren = true;
			}
		}
		private function updateOpponent(op:Player):void
		{
			if (op)
			{
				resultCanvas.opponentBox.valueName.text = op.name;
				resultCanvas.opponentBox.valueRanking.text = op.ranking.toString();
				resultCanvas.opponentBox.valuePoints.text = op.points.toString();
				resultCanvas.opponentBox.valueVictories.text = op.victories.toString();
				resultCanvas.opponentBox.valueLosses.text = op.losses.toString();
			}
			else
			{
				resultCanvas.opponentBox.valueName.text = "";
				resultCanvas.opponentBox.valueRanking.text = "";
				resultCanvas.opponentBox.valuePoints.text = "";
				resultCanvas.opponentBox.valueVictories.text = "";
				resultCanvas.opponentBox.valueLosses.text = "";
				selectedPlayer = null;
				
			}
		}
		private function onMouseOverTextField(ev:MouseEvent):void
		{
			for (var i:int = 0;i<ev.target.numChildren;++i)
			{
				ev.target.getChildAt(i).backgroundColor = 0x123456;
				ev.target.getChildAt(i).textColor = 0xFFFFFF;
			}
		}
		private function onMouseOutTextField(ev:MouseEvent):void
		{
			for (var i:int = 0;i<ev.target.numChildren;++i)
			{
				ev.target.getChildAt(i).backgroundColor = 0x777777;
				ev.target.getChildAt(i).textColor = 0;
					
			}
			
		}
		private function onMouseClickTextField(ev:MouseEvent):void
		{
			selectedTextFieldContainer = ev.target;
			selectedPlayer = players[textFields.getChildIndex(selectedTextFieldContainer as DisplayObject)]
			updateOpponent(selectedPlayer);
			
		}
		private function onButtonRaceClicked(ev:MouseEvent):void
		{
			if (selectedPlayer)
			{
				DRace.sfc.addEventListener(SFSEvent.onExtensionResponse,onExtensionResponse);
				//debo mandar un obj con las 2 id
				var obj:Object = {};
				obj.playerId = DRace.player.id;
				obj.opponentId = selectedPlayer.id;
						
				DRace.sfc.sendXtMessage("simpleExt","race",obj);
			}
		}
		private function onResultPopUpClosed(ev:MouseEvent):void
		{
			DRace.sfc.addEventListener(SFSEvent.onExtensionResponse,onExtensionResponse);
			
			var obj:Object = {};
			obj.userId = DRace.player.id;
			DRace.sfc.sendXtMessage("simpleExt","getRaceOpponents",obj);	
			
			this.playerListCanvas.mouseEnabled = true;
			this.playerListCanvas.mouseChildren = true;
			this.resultCanvas.mouseChildren = true;
			this.resultCanvas.mouseEnabled = true;
			updateOpponent(null);
			this.removeChild(resultPopUp);
		}
	
		
	}
}